const readline = require('readline-sync');// 引包

// 管理员
const users = [
    { username: 'zhangsan', password: '123' },
    { username: 'lisi', password: '456' }
];
// 学生
const studentsData = [
    { id: 1, name: 'zhao', age: '18', gender: 'male', score: '98' },
    { id: 2, name: 'qian', age: '20', gender: 'female', score: '100' },
    { id: 3, name: 'sun', age: '17', gender: 'male', score: '77' },
    { id: 4, name: 'li', age: '22', gender: 'female', score: '65' },
    { id: 5, name: 'zhou', age: '19', gender: 'male', score: '80' }
];

//登录函数
function login() {
    console.log('请输入账号');
    let user = readline.question();
    console.log('请输入密码');
    let pwd = readline.question();
    let result = users.filter(item => item.username === user && item.password === pwd)
    if (result.length) {//判断是否匹配到
        console.log(`登录成功，欢迎你${user}`);
        return result[0]

    } else {
        console.log('查无此人');
    }
}

//注册函数
function register() {
    let reg = /^[a-zA-Z][\w]{5,17}$/
    console.log('请输入注册账号');
    let newUser = readline.question();
    if (reg.test(newUser)) {
        let result = users.some(item => item.username === newUser);
        if (result) {
            console.log('账户已存在');
        } else {
            console.log('请输入密码');
            let newPwd = readline.question();
            users.push({ username: newUser, password: newPwd })
            console.log('注册成功');
            console.log(users);
        }

    } else {
        console.log('账号格式不对');
    }
}

//新增
function add() {
    console.log("新增学生姓名：");
    let name = readline.question();
    console.log("新增学生年龄：");
    let age = readline.question();
    console.log("新增学生性别：");
    let gender = readline.question();
    console.log("新增学生成绩：");
    let score = readline.question();
    let id = studentsData[studentsData.length - 1].id + 1;//id自增
    studentsData.push({ id, name, age, gender, score });//放入新的学生数据
    console.log('新增成功!');
    console.log(studentsData);

}

//删除
function remove() {
    console.log('请输入需要删除的id');
    let id = readline.question();
    let i;//保存id
    let result = studentsData.filter((item, index) => {
        if (item.id == id) {
            i = index;
            return item.id == id;
        }
    });
    if (result.length) {//删除除id以外的属性
        console.log('是否删除:', result[0], '? 1. 删除 2. 取消：');
        let opt = readline.question();
        switch (opt) {
            case '1':
                // studentsData.splice(i, 1);
                delete studentsData[i].name
                delete studentsData[i].age
                delete studentsData[i].gender
                delete studentsData[i].score
                console.log(studentsData);
                console.log('删除成功');
                break;
            case '2':
                console.log(886);
                return;

        }

    } else {
        console.log('查无此人');
    }
}

//查询函数
function check() {
    console.log('1.查询所有 2.分类查询 3.退出查询');
    let opt = readline.question();
    switch (opt) {
        case '1':
            console.log(studentsData);
            break;

        case '2':
            console.log('请输入查询项目:1.姓名 2.年龄 3.性别 4.成绩');
            let opt = readline.question();
            let type = '';
            switch (opt) {
                case '1': type = 'name'; break;
                case '2': type = 'age'; break;
                case '3': type = 'gender'; break;
                case '4': type = 'score'; break;
            }
            console.log('请输入查询内容');
            let content = readline.question();
            let result = studentsData.filter(item => item[type] == content);
            if (result.length) {
                console.log(result)
            } else {
                console.log('没有相关内容');
            }


            break;
        case '3':
            console.log('退出查询');
            break;
    }
}


//修改函数
function alter() {
    console.log('请输入需要修改的id');
    let id = readline.question();
    let result = studentsData.filter(item => item.id == id);
    let isFinish = false;
    if (result.length>0) {
        while (!isFinish) {
            console.log('请输入需要修改的项：1. 姓名 2. 年龄 3. 性别 4. 成绩 5.完成');
            let opt = readline.question();
            switch (opt) {
                case '1':
                    console.log(`当前姓名${result[0].name}`);
                    console.log('请输入修改的名字');
                    let name = readline.question();
                    result[0].name = name;
                    console.log(studentsData);
                    break;
                case '2':
                    console.log(`当前年龄${result[0].age}`);
                    console.log('请输入修改的年龄');
                    let age = readline.question();
                    result[0].age = age;
                    console.log(studentsData);
                    break;
                case '3':
                    console.log(`当前性别${result[0].gender}`);
                    console.log('请输入修改的性别');
                    let male = readline.question();
                    result[0].gender = male;
                    console.log(studentsData);
                    break;
                case '4':
                    console.log(`当前成绩${result[0].score}`);
                    console.log('请输入修改的成绩');
                    let score = readline.question();
                    result[0].score = score;
                    console.log(studentsData);
                    break;
                case '5':
                    isFinish = true;
                    console.log('修改完成');
                    break;
            }
        }

    }else{
        console.log('查无此人');
    }
}


//主函数
function main() {
    let user;
    while (true) {
        console.log('1.登录 2.注册 3.退出');
        let opt = readline.question();
        switch (opt) {
            case '1'://登录
                user = login()
                // console.log(user);
                break;
            case '2':
                register();
                break;
            case '3':
                console.log('退出系统');
                return;
        }
        let flag = true;
        if (user) {
            while (flag) {
                console.log(' 1.新增  2.删除  3.查询  4.修改  5.退出');
                let opt = readline.question();
                switch (opt) {
                    case '1'://新增
                        add();
                        break;
                    case '2'://删除
                        remove();
                        break;
                    case '3'://查询
                        check();
                        break;
                    case '4'://修改
                        alter();
                        break;
                    case '5'://退出
                        flag = false;
                        console.log('返回上一层');
                        break;
                }
            }

        }

    }


}
main()
